<?php
/*
 * @Author: Terry
 * @Date: 2024-11-28 10:14:27
 * @LastEditors: Terry
 * @LastEditTime: 2024-12-14 14:03:31
 * @Description: ai摄像头接口
 * @FilePath: \auxface_certificate\app\openApi\controller\v1\AICamera.php
 */

declare (strict_types = 1);

namespace app\openApi\controller\v1;

use app\platform\command\AicameraSync;
use libs\AicameraClient;
use think\Request;
use think\facade\Db;
use libs\Image;
use libs\File;
use libs\ALiOss;

class AICamera{
    /**
     * 车辆预警测速
     */
    public function speedCheck(Request $request){
        $data = $request->data;
        $AicameraClient = new AicameraClient();
        $data['func'] = 'speedCheck';
        $AicameraClient->publish($data);
        return result(200,$data,'');
      }
    /**
     * @Description: ai预警
     * @Author: Terry
     * @Date: 2024-11-28 10:15:04
     */    
    public function alert(Request $request){
        $headerMap = $request->header();
        // 获取请求体
        $data = $request->data;
        $type = $headerMap['type'];
        foreach ($data['extend']['label'] as $key => $value) {
            file_put_contents("data_{$type}_{$value}.txt",json_encode($data));
            file_put_contents("header_{$type}_{$value}.txt",json_encode($headerMap));
        }
        $messageId = $headerMap['message-id'];
        // 返回结果
        $result=[
            'message_id' =>$messageId,
        ];
        $result = json_encode($result);

        // 判断时间和签名
        $timestamp = $headerMap['timestamp'];
        $signature = $headerMap['signature'];
        if(!$timestamp || !$signature) return $result;

    
        $AicameraClient = new AicameraClient();
        $data['headerMap'] = $headerMap;
        $data['func'] = 'alert';
        $AicameraClient->publish($data);
        
        return json($result);
    }
}
